#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::weak_ptr;
using std::shared_ptr;

void test(){
    weak_ptr<int> wp;
    shared_ptr<int> sp(new int(20));
    wp = sp;
    //如果希望能够使用，需要将wp升级为sp
    //lock函数可以将wp升级为sp
    shared_ptr<int> sp2 = wp.lock();
    //把shared_ptr放到if条件语句中，作用就是判断所托管的指针是否为空
    if(sp2){
        cout << "提升成功" << endl;
    }
    cout << wp.use_count() << endl;

    if(wp.expired()){
        cout << "该空间已经被回收" << endl;
    }else {
        cout << "该空间引用计数不为0" << endl;
    }
}

int main()
{
    test();
    return 0;
}

